package com.example.mkio.androidpractice;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mkio.androidpractice.utils.BitmapUtils;
import com.example.mkio.androidpractice.utils.FilePathUtils;
import com.example.mkio.androidpractice.utils.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TakePhotoActivity extends AppCompatActivity implements View.OnClickListener {

    private final int CAMERA_USER = 1001;
    private final int GALLERY_USER = 3001;
    private Context mContext;

    private List<String> userPathList = new ArrayList<>();

    private LinearLayout mTakePhoto;
    private ImageView mPhoto;
    private String imagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_take_photo);
        mPhoto = (ImageView) findViewById(R.id.iv_apply_seal_add_user_pic);
        mTakePhoto = (LinearLayout) findViewById(R.id.ll_apply_seal_user_photo);
        mTakePhoto.setOnClickListener(this);
        mPhoto.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_apply_seal_add_user_pic:
                showPicSelectDialog();
                break;
        }
    }

    private void showPicSelectDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(mContext, R.layout.dialog_choose_image, null);
        builder.setView(view);
        final AlertDialog dialog = builder.create();
        TextView tvgGallery = (TextView) view.findViewById(R.id.tv_gallery);
        TextView tvCamera = (TextView) view.findViewById(R.id.tv_camera);
        //获取相册图片
        tvgGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent picture = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(picture, GALLERY_USER);
            }
        });
        //相机拍照
        tvCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File imageFile = FilePathUtils.getImageFile();
                imagePath = imageFile.getAbsolutePath();
                LogUtil.d("ApplySeal Camera Pic Path: " + imagePath);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
                startActivityForResult(intent, CAMERA_USER);
            }
        });
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK /*&& requestCode != ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP*/) {
            return;
        }
        switch (requestCode) {
            case GALLERY_USER:
                Uri uri = data.getData();
                String[] filePathColumns = {MediaStore.Images.Media.DATA};
                Cursor c = this.getContentResolver().query(uri, filePathColumns, null, null, null);
                if (c == null) {
                    return;
                }
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePathColumns[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                userPathList.clear();
                userPathList.add(picturePath);
                Bitmap bitmapWithGallery = BitmapUtils.getScaleBitmap(this, picturePath);
                mPhoto.setImageBitmap(bitmapWithGallery);
                break;
            case CAMERA_USER:
                userPathList.clear();
                userPathList.add(imagePath);
                Bitmap bitmapWithCamera = BitmapUtils.getScaleBitmap(this, imagePath);
                mPhoto.setImageBitmap(bitmapWithCamera);
                break;
        }
    }
}